// Package sdk 提供硬件UDP通信SDK的主要接口
package sdk

import (
	"fmt"
	"time"
	
	"inkjet168x-sdk-go/sdk/client"
	"inkjet168x-sdk-go/sdk/protocol/common"
	"inkjet168x-sdk-go/sdk/protocol/text"
	"inkjet168x-sdk-go/sdk/protocol/barcode"
	"inkjet168x-sdk-go/sdk/transport"
)

// SDK 主要SDK结构体
type SDK struct {
	client *client.HardwareClient
	factory *common.DefaultProtocolFactory
}

// Config SDK配置
type Config struct {
	// 硬件设备地址
	Address string
	
	// 连接超时时间
	Timeout time.Duration
	
	// 传输配置
	Transport transport.Config
}

// NewSDK 创建新的SDK实例
func NewSDK(config Config) (*SDK, error) {
	// 设置默认值
	if config.Timeout == 0 {
		config.Timeout = 5 * time.Second
	}
	
	// 创建传输层
	transport := transport.NewUDPTransport(config.Transport)
	
	// 创建硬件客户端
	hwClient := client.NewHardwareClient(transport)
	
	// 创建协议工厂
	factory := common.NewDefaultProtocolFactory()
	
	// 注册协议
	factory.RegisterProtocol(common.ProtocolTypeText, text.NewProtocol)
	factory.RegisterBuilder(common.ProtocolTypeText, text.NewBuilder)
	
	factory.RegisterProtocol(common.ProtocolTypeBarcode, barcode.NewProtocol)
	factory.RegisterBuilder(common.ProtocolTypeBarcode, barcode.NewBuilder)
	
	sdk := &SDK{
		client:  hwClient,
		factory: factory,
	}
	
	// 连接到硬件设备
	if err := sdk.Connect(config.Address); err != nil {
		return nil, fmt.Errorf("连接硬件设备失败: %w", err)
	}
	
	return sdk, nil
}

// Connect 连接到硬件设备
func (s *SDK) Connect(address string) error {
	return s.client.Connect(address)
}

// Disconnect 断开连接
func (s *SDK) Disconnect() error {
	return s.client.Disconnect()
}

// IsConnected 检查连接状态
func (s *SDK) IsConnected() bool {
	return s.client.IsConnected()
}

// GetLocalAddress 获取本地地址
func (s *SDK) GetLocalAddress() string {
	return s.client.GetLocalAddress()
}

// GetRemoteAddress 获取远程地址
func (s *SDK) GetRemoteAddress() string {
	return s.client.GetRemoteAddress()
}

// SendProtocol 发送协议
func (s *SDK) SendProtocol(protocol common.Protocol) error {
	return s.client.SendProtocol(protocol)
}

// SendProtocols 批量发送协议
func (s *SDK) SendProtocols(protocols []common.Protocol) (int, error) {
	return s.client.SendProtocols(protocols)
}

// CreateTextBuilder 创建文本协议构建器
func (s *SDK) CreateTextBuilder() *text.Builder {
	builder, _ := s.factory.CreateBuilder(common.ProtocolTypeText)
	return builder.(*text.Builder)
}

// CreateBarcodeBuilder 创建条码协议构建器
func (s *SDK) CreateBarcodeBuilder() *barcode.Builder {
	builder, _ := s.factory.CreateBuilder(common.ProtocolTypeBarcode)
	return builder.(*barcode.Builder)
}

// ParseProtocol 解析协议数据
func (s *SDK) ParseProtocol(data []byte) (common.Protocol, error) {
	return s.factory.ParseProtocol(data)
}

// GetSupportedProtocols 获取支持的协议类型
func (s *SDK) GetSupportedProtocols() []common.ProtocolType {
	return s.factory.GetSupportedTypes()
}

// GetStatistics 获取统计信息
func (s *SDK) GetStatistics() client.Statistics {
	return s.client.GetStatistics()
}

// 便捷方法

// SendText 发送简单文本
func (s *SDK) SendText(text string, x, y uint32) error {
	protocol, err := s.CreateTextBuilder().
		SetText(text).
		SetPosition(x, y).
		Build()
	if err != nil {
		return err
	}
	return s.SendProtocol(protocol)
}

// SendQRCode 发送二维码
func (s *SDK) SendQRCode(text string, x, y, size uint32) error {
	protocol, err := s.CreateBarcodeBuilder().
		AsQRCode().
		SetText(text).
		SetPosition(x, y).
		SetSize(size, size).
		Build()
	if err != nil {
		return err
	}
	return s.SendProtocol(protocol)
}

// SendBarcode 发送条码
func (s *SDK) SendBarcode(barcodeType barcode.Type, text string, x, y, width, height uint32) error {
	protocol, err := s.CreateBarcodeBuilder().
		SetBarcodeType(barcodeType).
		SetText(text).
		SetPosition(x, y).
		SetSize(width, height).
		Build()
	if err != nil {
		return err
	}
	return s.SendProtocol(protocol)
}

// TextToBytes 将文本对象转换为协议二进制数组
func (s *SDK) TextToBytes(textObj *text.Protocol) ([]byte, error) {
	if textObj == nil {
		return nil, fmt.Errorf("文本对象不能为空")
	}
	
	// 调用协议的Marshal方法获取二进制数据
	return textObj.Marshal()
}

// CreateTextProtocol 创建文本协议对象
func (s *SDK) CreateTextProtocol(content string, x, y, fontSize, fontWidth uint32) (*text.Protocol, error) {
	return s.CreateTextBuilder().
		SetText(content).
		SetPosition(x, y).
		SetFontSize(fontSize).
		SetFontWidth(fontWidth).
		Build()
}